﻿Public Class frmLocation
    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        ClearText()
        Me.txtLocID.Text = AutoID4Access("SELECT LocationID FROM tbl_location", "tbl_location") + 1
        Me.btbUpdate.Enabled = False
        Me.btnSave.Enabled = True
    End Sub
    Sub ClearText()
        Me.txtLocID.Text = ""
        Me.txtLocaName.Text = ""
        Me.txtNote.Text = ""
        Me.txtLocaName.Focus()
        Me.txtLocID.Text = AutoID4Access("SELECT LocationID FROM tbl_location", "tbl_location") + 1
        Me.txtLocID.Enabled = False
    End Sub

    Private Sub frmLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Connect()
        LoadDataToLVLocation("SELECT * FROM tbl_location", LVLocation)
        Me.txtLocID.Text = AutoID4Access("SELECT LocationID FROM tbl_location", "tbl_location") + 1
        Me.txtLocID.Enabled = False

        Me.btbUpdate.Enabled = False
        Me.btnSave.Enabled = False

    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (Me.txtLocID.Text = "") Then
            MsgBox("Please Enter Value !", MsgBoxStyle.Exclamation, "Invalid Value")
            Me.txtLocID.Focus()
        ElseIf (Me.txtLocaName.Text = "") Then
            MsgBox("Please Enter Value!", MsgBoxStyle.Exclamation, "Invalid Value")
            Me.txtLocaName.Focus()
        Else
            Dim SQL As String = "INSERT INTO tbl_location(LocationID,LocationName,LocationNote)" & _
                              "VALUES (" & Me.txtLocID.Text & ",'" & Me.txtLocaName.Text & "','" & Me.txtNote.Text & "')"
            SaveDataToAccess(SQL)
            ClearText()
            LoadDataToLVLocation("SELECT * FROM tbl_location", LVLocation)
        End If
    End Sub

    Private Sub btbUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbUpdate.Click

        Dim SQL As String = "UPDATE tbl_location SET LocationName='" & Me.txtLocaName.Text & _
                                           "', LocationNote='" & Me.txtNote.Text & _
                                           "' WHERE LocationID=" & Me.txtLocID.Text
        SaveDataToAccess(SQL)
        LoadDataToLVLocation("SELECT * FROM tbl_location", LVLocation)
        ClearText()

    End Sub

    Private Sub LVLocation_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LVLocation.MouseDoubleClick
        Dim i As Integer
        For i = 0 To Me.LVLocation.Items.Count - 1
            If LVLocation.Items(i).Selected = True Then
                txtLocID.Text = LVLocation.Items(i).Text
                Me.txtLocaName.Text = LVLocation.Items(i).SubItems(1).Text
                Me.txtNote.Text = LVLocation.Items(i).SubItems(2).Text
            End If
        Next
        Me.btbUpdate.Enabled = True
        Me.btnSave.Enabled = False
    End Sub

    Private Sub LVLocation_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LVLocation.SelectedIndexChanged

    End Sub
End Class